//显示购物车功能
// let uid = localStorage.getItem("uid");
let uid = 63073;
loadListCar();
function loadListCar() {
    console.log(uid);
    axios.get(CartListAPI, { params: { id: uid } }).then((res) => {
        console.log(res);
        let pObjArr = res.data.data;
        console.log(pObjArr);
        let resArr = pObjArr.map(
            (pObj) =>
                `
                <ul class="car-list">
                    <li><input type="checkbox" class="check single-ipt" onchange = "singleBtn(${pObj.pnum},${pObj.pprice},this)"/></li>
                    <li><img src="${pObj.pimg}" alt=""><a href="item.html">${pObj.pname}</a></li>
                    <li class = "pprice-ipt">${pObj.pprice}</li>
                    <li class = "anniu">
                        <button onclick =  "subBtn(${pObj.pid},this)"> - </button>
                        <input type="text" value=${pObj.pnum} class = "ipts pnum-ipt" onblur = "iptBtn(${pObj.pid},this)">
                        <button onclick =  "addBtn(${pObj.pid},this)">+</button>
                    </li>
                    <li>${pObj.pprice * parseInt(pObj.pnum)}.00</li>
                    <li onclick =  "delBtn(${pObj.pid},this)">删除</li>
              </ul>
            `

        );
        let content = document.querySelector(".content1")
        content.innerHTML = resArr.join('');
    });
}

//给加按钮添加添加点击事件
async function addBtn(pid, btn) {
    console.log("加");
    console.log(btn);
    let ipt = btn.previousElementSibling;
    console.log(ipt.value);
    let pnum = parseInt(ipt.value) + 1;
    let uid = localStorage.getItem("uid");
    let params = { uid, pid, pnum };
    let data = await axios.get(CartUpdateAPI, { params });
    console.log(data);
    ipt.value = pnum;
    console.log(ipt.value);
    loadListCar();
    contFN();
}

//给减按钮添加添加点击事件
async function subBtn(pid, btn) {
    console.log("减");
    console.log(btn);
    let ipt = btn.nextElementSibling;
    if (parseInt(ipt.value) == 1) {
    } else {
        let pnum = parseInt(ipt.value) - 1;
        let uid = localStorage.getItem("uid");
        let params = { uid, pid, pnum };
        let data = await axios.get(CartUpdateAPI, { params });
        console.log(data);
        ipt.value = pnum;
        loadListCar();
          contFN();
    }
}

//给输入框添加事件，失焦时触发，获取输入框的值改变购物车里面的数量
async function iptBtn(pid, btn) {
    console.log(222);
    console.log(btn);
    let ipt = btn;
    console.log(ipt.value);
    console.log(pid);
    let pnum = parseInt(ipt.value);
    let uid = localStorage.getItem("uid");
    let params = { uid, pid, pnum };
    let data = await axios.get(CartUpdateAPI, { params });
    console.log(data);
    ipt.value = pnum;
    loadListCar();
    contFN();
}

//给删除添加点击事件
async function delBtn(pid) {
    console.log("删除");
    let uid = localStorage.getItem("uid");
    let params = { uid, pid };
    let data = await axios.get(CartDeleteAPI, { params });
    console.log(data);
    loadListCar();
    contFN();
}
//给单选按钮添加点击事件，选中单选按钮时计算商品总价改变span里面的内容
function singleBtn() {
    console.log("单选");
    contFN();
    let singleAll = document.querySelectorAll(".single-ipt");
    console.log(singleAll);
    let arr = [...singleAll];
    let flag = arr.every((v) => v.checked == true);
    let allIpt = document.querySelector(".allIpt");
    allIpt.checked = flag;
}

//给全选按钮添加点击事件，选中单选按钮时计算商品总价改变span里面的内容
function checkAll() {
    console.log("全选");
    let singleAll = document.querySelectorAll(".single-ipt");
    let allIpt = document.querySelector(".allIpt");
    console.log(singleAll);
    let arr = [...singleAll];
    let flag = allIpt.checked;
    let resArr = arr.forEach((v) => (v.checked = flag));
    contFN();
}

  function contFN() {
    let singleAll = document.querySelectorAll(".single-ipt");
    console.log(singleAll); //伪数组
    // 转化为真数组进行过滤
    let arr = [...singleAll];
    let resArr = arr.filter((v) => v.checked == true);
    console.log(resArr);
    //遍历数组获取数据计算总价
    let num = 0;
    resArr.forEach((singleIpt) => {
      let ul = singleIpt.parentNode.parentNode;
    
      let pprice = ul.querySelector(".pprice-ipt");
      let pnum = ul.querySelector(".pnum-ipt");
      console.log(pprice.innerHTML);
      console.log(pnum.value);
      let cont = pprice.innerHTML * pnum.value;
      num = num + cont;
    });
    let zj = document.querySelector(".zj")
    zj.innerHTML = num;
  }
